##  ITEM RESPONSE THEORY: PROPOSED GUESSING MODEL
##  MODEL FOR MEASURMENT OF POLITICAL KNOWLEDGE
##  TO MODEL DIFFERENTIAL ITEM FUNCTIONING, LET ITEM PARAMETERS VARY ACROSS GROUPS, SUCH AS GENDER, UNDER A MULTILEVEL FRAMEWORK.

model {
	# LOOP OVER N RESPONDENTS
	for (i in 1:N) {        
		# LOOP OVER K ITEMS
	    for (k in 1:K) {    
	    	# LOGISTIC MODEL FOR POLITICAL KNOWLEDGE
	    	y[i,k] ~ dbern (p[i,k])
	    	logit(p.ability[i,k]) <- beta[group[i],k]*(theta[i] - alpha[group[i],k])
	    	
	    	# GUESSING FUNCTION
	    	guess[i,k] <- exp(b[group[i],k]*(theta[i] - alpha[group[i],k]))/(1 + (M-1)*exp(b[group[i],k]*(theta[i] - alpha[group[i],k])))   	
	    	
	    	# PROBABILITY OF SUCCESS	
	    	p[i,k] <- p.ability[i,k] + (1 - p.ability[i,k])*guess[i,k]
	    }    
	    # DISTRIBUTIONAL ASSUMPTION FOR THE LATENT TRAIT
	    theta[i] ~ dnorm (0, 1)
    }    

	# FOR IDENTIFICATION, THE PARAMETER ESTIMATES OF ONE ITEM IS HELD CONSTANT ACROSS GROUPS
    for (j in 1:J) {
    	beta[j,1] <- 1.5
    	alpha[j,1] <- -3
    	b[j,1] <- 0.5
    }
             
	# DISTRIBUTIONS OF ITEM PARAMETERS
    for (k in 2:K) {
    	for (j in 1:J) {
    		beta[j,k] ~ dnorm (1, 0.5) T(0,)  # DISCRIMINATION PARAMETER; USUALLY [0.5, 3]
    		alpha[j,k] ~ dnorm (0, tau.alpha)  # DIFFICULTY PARAMETER
    		b[j,k] ~ dunif (0, 1)
    	}
    }   

	tau.alpha ~ dgamma (0.01, 0.01) # shape and rate;
	sigma.alpha <- 1/sqrt(tau.alpha)

} # END OF MODEL


